package com.wh.wisdomsite.xbox.server.filter;

import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.server.session.ServerSessionUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * 配置过滤器
 * @Package com.wh.wisdomsite.xbox.server.filter
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class AdminFilter implements Filter {


    private static final Log logger = LogFactory.getLog(AdminFilter.class);

    private static String ADMIN_APPSECRET_URL = "";

    private static String SESSION_USER_URL = "";

    public String[] NoFilter = new String[]{"login.do", "generatCode.do", "grCodePage.do", "register.do",
            "getCommonDictOptions.do", "loginNameIsExists.do", "/test/","/alarmSign/save.do","apiUtil/doGet.do",
            "userLogin/ythLogin.do", "ythGetProjectList.do", "projectView.do"};


    /**
     * 构造函数
     */
    public AdminFilter() {

    }


    /**
     * 销毁
     */
    public void destroy() {
        logger.info("destroy");
    }


    /**
     * 初始化
     * @param filterConfig
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    /**
     * 执行拦截
     * @param servletRequest
     * @param servletResponse
     * @param chain
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain){
        //获取HttpServletRequest
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //获取HttpServletResponse
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        try{
            //设置ContentType
            response.setContentType("text/html; charset=UTF-8");
            //设置允许跨域请求
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            //获取session
            HttpSession session = request.getSession(true);
            //获取请求URL地址
            String url = request.getServletPath();
            //不拦截登陆请求
            if(checkUrl(url)){
                chain.doFilter(request, response);
                return;
            }
           //判断会话
           ServerSessionUser serverSessionUser = (ServerSessionUser) session.getAttribute(Constants.SERVER_SESSION_USER);
           if(null == serverSessionUser){
                logger.info("会话丢失-->>serverSessionUser: " + serverSessionUser);
               SESSION_USER_URL = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
                logger.info("重定向-->>URL:"  + SESSION_USER_URL);
                response.getWriter().print("<script> window.top.location.href = '" + SESSION_USER_URL + "';</script>");
                return ;
            }
            chain.doFilter(request, response);
        }catch (IOException e){
            e.printStackTrace();
            logger.error("doFilter-->>IOException" + e.getMessage());
        }catch (ServletException e){
            e.printStackTrace();
            logger.error("doFilter-->>ServletException" + e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("doFilter-->>Exception" + e.getMessage());
       }
    }





    /***
     * 检查url是否可以访问
     * @param url
     * @return
     */
    public Boolean checkUrl(String url){
        Boolean isOk = false;
        for(String ok: NoFilter){
            if(url.indexOf(ok)!=-1){
                isOk = true;
                return isOk;
            }
        }
        return isOk;
    }

}
